<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
<!--      <el-form-item label="父冷库" prop="parentId">-->
<!--        <el-select v-model="queryParams.parentId" placeholder="请选择所属冷库">-->
<!--          <el-option-->
<!--            v-for="cold in parentCold"-->
<!--            :key="cold.coldId"-->
<!--            :label="cold.coldName"-->
<!--            :value="cold.coldId">-->
<!--          </el-option>-->
<!--        </el-select>-->
<!--      </el-form-item>-->
      <el-form-item label="父冷库" prop="coldName">
        <el-input
          v-model="queryParams.coldName"
          placeholder="请输入父冷库名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="区域名称" prop="subName">
        <el-input
          v-model="queryParams.subName"
          placeholder="请输入区域名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="区域描述" prop="description">
        <el-input
          v-model="queryParams.description"
          placeholder="请输入区域描述"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="最小容积" prop="capacity1">
        <el-input-number
          v-model="queryParams.capacity1"
          placeholder="最小容积"
          clearable
          controls-position="right" :min="0" />
      </el-form-item>
      <el-form-item label="最大容积" prop="capacity2">
        <el-input-number
          v-model="queryParams.capacity2"
          placeholder="最大容积"
          clearable
          controls-position="right" :min="0" />
      </el-form-item>
      <el-form-item label="冷库类型" prop="coldId">
        <el-select v-model="queryParams.coldId" placeholder="请选择所属冷库" @change="onColdTypeChanged">
          <el-option
            v-for="cold in coldType"
            :key="cold.coldTypeId"
            :label="cold.typeName"
            :value="cold.coldTypeId">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="最小价格" prop="dailyPrice1">
        <el-input-number
          v-model="queryParams.dailyPrice1"
          placeholder="最小价格"
          clearable
          controls-position="right" :min="0" />
      </el-form-item>
      <el-form-item label="最大价格" prop="dailyPrice2">
        <el-input-number
          v-model="queryParams.dailyPrice2"
          placeholder="最大价格"
          clearable
          controls-position="right" :min="0" />
      </el-form-item>
      <el-form-item label="负责人" prop="people">
        <el-input
          v-model="queryParams.people"
          placeholder="请输入负责人姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
<!--      <el-form-item label="创建人" prop="createBy">-->
<!--        <el-input-->
<!--          v-model="queryParams.createBy"-->
<!--          placeholder="请输入创建人姓名"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="修改人" prop="updateBy">-->
<!--        <el-input-->
<!--          v-model="queryParams.updateBy"-->
<!--          placeholder="请输入负责人姓名"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
      <el-form-item label="创建时间">
        <el-date-picker
          v-model="dateRange"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="岗位状态" clearable>
          <el-option
            v-for="dict in dict.type.lg_sub_cold_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">查询</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['storage:smallCold:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['storage:smallCold:update']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['storage:smallCold:delete']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['storage:smallCold:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="postList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="编号" align="center" width="80">
        <template slot-scope="scope">
          {{ (scope.$index + 1) + (queryParams.pageNum - 1) * queryParams.pageSize }}
        </template>
      </el-table-column>
      <el-table-column label="冷库区域id" align="center" prop="subId" />
<!--      <el-table-column label="父冷库名称" align="center" prop="coldName" />-->
      <el-table-column label="冷库名称" align="center" prop="coldName">
        <template slot-scope="scope">
          <el-button type="text" @click="getMap(scope.row)">{{ scope.row.coldName }}</el-button>
        </template>
      </el-table-column>
      <el-table-column label="区域名称" align="center" prop="subName" />
      <el-table-column label="描述" align="center" prop="description" />
      <el-table-column label="容积" align="center" prop="capacity">
        <template slot-scope="scope">
          {{ scope.row.capacity }} 立方米
        </template>
      </el-table-column>
      <el-table-column label="单日价格" align="center" prop="dailyPrice">
        <template slot-scope="scope">
          {{ scope.row.dailyPrice }} 元
        </template>
      </el-table-column>
      <el-table-column label="区域类型" align="center" prop="typeName" />
      <el-table-column label="当前温度" align="center" prop="temperature">
        <template slot-scope="scope">
          {{ scope.row.temperature ? `${scope.row.temperature} 摄氏度` : scope.row.temperature }}
        </template>
      </el-table-column>
      <el-table-column label="当前湿度" align="center" prop="humidity">
        <template slot-scope="scope">
          {{ scope.row.humidity ? `${scope.row.humidity} RH` : scope.row.humidity }}
        </template>
      </el-table-column>
      <el-table-column label="负责人" align="center" prop="userName" />
<!--      <el-table-column label="当前所属" align="center" prop="currentOwner" />-->
<!--      <el-table-column label="创建人" align="center" prop="createBy" />-->
<!--      <el-table-column label="修改人" align="center" prop="updateBy" />-->
      <el-table-column label="状态" align="center" prop="status">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.lg_sub_cold_status" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="修改时间" align="center" prop="updateTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.updateTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['storage:smallCold:update']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['storage:smallCold:delete']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getEvery"
    />

    <!-- 添加或修改岗位对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body
    style="padding: 0;margin: 0">
      <div>
              <el-form ref="form" :model="form" :rules="rules" label-width="80px">
                <el-form-item label="父冷库" prop="parentId">
                  <el-select v-model="form.parentId" placeholder="请选择所属冷库" @change="onParentColdChanged">
                    <el-option
                      v-for="cold in parentCold"
                      :key="cold.coldId"
                      :label="cold.coldName"
                      :value="cold.coldId">
                    </el-option>
                  </el-select>
                  <el-button  id = "openSome" @click="getSome">
                    选择
                  </el-button>
                </el-form-item>
                <el-form-item label="负责人" prop="coldUserName">
                  <el-input v-model="coldUserName" readonly/>
                </el-form-item>
                <el-form-item label="区域名称" prop="subName">
                  <el-input v-model="form.subName" placeholder="请输入区域名称" />
                </el-form-item>
                <el-form-item label="区域描述" prop="description">
                  <el-input v-model="form.description" placeholder="请输入区域描述" />
                </el-form-item>
                <el-form-item label="区域容积" prop="capacity">
                  <el-input-number v-model="form.capacity" controls-position="right" :min="0" />
                </el-form-item>
                <el-form-item label="冷库类型" prop="coldId">
                  <el-select v-model="form.coldId" placeholder="请选择所属冷库" @change="onColdTypeChanged">
                    <el-option
                      v-for="cold in coldType"
                      :key="cold.coldTypeId"
                      :label="cold.typeName"
                      :value="cold.coldTypeId">
                    </el-option>
                  </el-select>
                </el-form-item>
                <el-form-item label="区域状态" prop="status">
                  <el-radio-group v-model="form.status">
                    <el-radio
                      v-for="dict in dict.type.lg_sub_cold_status"
                      :key="dict.value"
                      :label="dict.value"
                    >{{dict.label}}</el-radio>
                  </el-radio-group>
                </el-form-item>
              </el-form>
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <el-dialog :title="title" :visible.sync="open2" width="1000px"  append-to-body>
      <baidu-map
        class="map2"
        :center="{ lng: mapX, lat: mapY }"
        :zoom="15"
        :scroll-wheel-zoom="true"
        mapType="BMAP_NORMAL_MAP"
      >
        <!-- 添加地图控件 -->
        <bm-navigation anchor="BMAP_ANCHOR_TOP_RIGHT"></bm-navigation>
        <bm-scale anchor="BMAP_ANCHOR_BOTTOM_RIGHT"></bm-scale>
        <bm-city-list anchor="BMAP_ANCHOR_TOP_LEFT"></bm-city-list>

        <!-- 添加定位控件 -->
        <bm-geolocation
          anchor="BMAP_ANCHOR_BOTTOM_RIGHT"
          :showAddressBar="true"
          :autoLocation="true"
        ></bm-geolocation>

        <!-- 添加路况图层 -->
        <bm-traffic :type="0"></bm-traffic>

        <!-- 添加卫星图层（可选） -->
        <bm-overview-map anchor="BMAP_ANCHOR_BOTTOM_RIGHT"></bm-overview-map>
        <bm-marker
          :position="{ lng: mapX, lat: mapY }"
        ></bm-marker>
      </baidu-map>
      <div slot="footer" class="dialog-footer" style="padding-left: 330px;padding-right: 0">
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <!-- 弹出框 -->
    <el-dialog :title="title3" :visible.sync="open3" width="1000px" append-to-body>
      <el-table :data="parentColdList" style="width: 100%">
        <el-table-column label="编号" align="center" width="80">
          <template slot-scope="scope">
            {{ (scope.$index + 1) + (queryParams.pageNum - 1) * queryParams.pageSize }}
          </template>
        </el-table-column>
        <el-table-column prop="coldName" label="冷库名称" align="center"></el-table-column>
        <el-table-column prop="coldDescript" label="冷库描述" align="center"></el-table-column>
        <el-table-column prop="address" label="冷库位置" align="center"></el-table-column>
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              icon="el-icon-delete"
              @click="thisOk(scope.row)"
              v-hasPermi="['storage:smallCold:delete']"
            >选择</el-button>
          </template>
        </el-table-column>
      </el-table>

      <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="confirmSelection">确认</el-button>
        <el-button @click="cancelSelection">取消</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import {
  getAll,
  getParentCold,
  getColdType,
  getManagerByParentId,
  addSmallCold,
  getSmallColdById,
  updateSmallCold,
  listPost,
  getAllCount,
  getParentColdAll,
  getPost,
  delPost,
  addPost,
  updatePost,
  deleteSmallCold
} from "@/api/storage/smallCold";
import store from "@/store";
export default {
  name: "Post",
  dicts: ['sys_normal_disable','lg_sub_cold_status'],
  data() {
    return {
      userId:store.state.user.id, //将缓存的用户id赋值给userId
      userName:store.state.user.name, //将缓存的用户id赋值给userId
      // 遮罩层
      loading: true,
      //默认状态
      nowStatus: "1",
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 岗位表格数据
      postList: [],
      // 父冷库id与名字信息
      parentCold: [],
      //存储父冷库
      parentColdList: "",
      //冷库类型与名字信息
      coldType: [],
      // 日期范围
      dateRange: [],
      mapX: "",
      mapY: "",
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      //是否显示弹出层2
      open2: false,
      open3: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        // parentId: undefined,
        coldName: undefined,
        subName: undefined,
        description: undefined,
        capacity1: undefined,
        capacity2: undefined,
        dailyPrice1: undefined,
        dailyPrice2: undefined,
        coldId: undefined,
        people: undefined,
        createBy: undefined,
        updateBy: undefined,
        beginTime: undefined,
        endTime: undefined,
        status: undefined,
      },
      coldUserName: "",
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        postName: [
          { required: true, message: "岗位名称不能为空", trigger: "blur" }
        ],
        postCode: [
          { required: true, message: "岗位编码不能为空", trigger: "blur" }
        ],
        postSort: [
          { required: true, message: "岗位顺序不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    // this.getList();
    this.getEvery();
    //获得父冷库一些信息，方便制作下拉框
    this.getColdStorge();
    //获得冷库类型的一些相关信息，方便制作下拉框
    this.getColdType();
    this.getParentColdAllSome();
  },
  methods: {
    thisOk(some){
      this.form.coldName = some.coldName;
      this.form.parentId = some.coldId;
      this.onParentColdChanged();
      this.open3 = false;
    },
    cancelSelection(){
      this.open3 = false;
    },
    confirmSelection(){
      this.form.managerId = this.nowManager.userId;
      this.open3 = false;
    },
    //弹出页面
    getSome(){
      this.open3 = true;
    },
    //获得父冷库所有信息
    getParentColdAllSome(){
      getParentColdAll().then(response => {
        this.parentColdList = response.data;
        console.log(this.parentColdList);
      });
    },
    //点击冷库名字弹出地图
    getMap(some){
      this.open2 = true;
      this.mapX = some.coordinateX;
      this.mapY = some.coordinateY;
      // console.log(this.mapX);
    },
    /** 查询数据 */
    getEvery(){
      this.loading = true;
      this.queryParams.beginTime = this.dateRange[0];
      this.queryParams.endTime = this.dateRange[1];
      getAll(this.queryParams).then(response => {
        console.log(this.queryParams);
        this.postList = response.data;
        this.getTotalSome();
        this.loading = false;
      });
    },
    /** 获得数据条数 */
    getTotalSome(){
      this.queryParams.beginTime = this.dateRange[0];
      this.queryParams.endTime = this.dateRange[1];
      getAllCount(this.queryParams).then(response => {
        this.total = response.data;
      });
    },
    /** 获得父冷库信息 */
    getColdStorge(){
      getParentCold().then(response => {
        this.parentCold = response.data;
      });
    },
    /** 获得冷库类型信息 */
    getColdType(){
      getColdType().then(response => {
        this.coldType = response.data;
      });
    },
    /** 给区域类型赋值 */
    onColdTypeChanged(){
      for (var i = 0; i < this.coldType.length; i++){
        if (this.coldType[i].coldTypeId == this.form.cold_id){
          this.form.coldId = this.coldType[i].coldTypeId;
          break;
        }
      }
    },
    /** 获取父冷库名字 */
    onParentColdChanged(){
      var parentId = this.form.parentId;
      for (var i = 0; i < this.parentCold.length; i++){
        if (this.parentCold[i].coldId == parentId){
          this.form.coldName = this.parentCold[i].coldName;
          this.getManagerByColdNameSome();
          break;
        }
      }
    },
    /** 获取冷库管理员名字 */
    getManagerByColdNameSome(){
      getManagerByParentId(this.form.parentId).then(response => {
        this.coldUserName = response.data.userName;
        this.form.managerId = response.data.userId;
      });
    },
    /** 查询岗位列表 */
    // getList() {
    //   this.loading = true;
    //   listPost(this.queryParams).then(response => {
    //     // this.postList = response.rows;
    //     // this.total = response.total;
    //     this.loading = false;
    //   });
    // },
    // 取消按钮
    cancel() {
      this.open = false;
      this.open2 = false;
      this.reset();
    },
    // 表单重置
    reset() {
      // this.form = {
      //   postId: undefined,
      //   postCode: undefined,
      //   postName: undefined,
      //   postSort: 0,
      //   status: "0",
      //   remark: undefined
      // };
      this.form = {};
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getEvery();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.subId)
      this.single = selection.length!=1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.coldUserName="";
      this.reset();
      this.open = true;
      this.form.status = this.nowStatus.toString();
      this.title = "添加冷库区域";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const subId = row.subId || this.ids
      getSmallColdById(subId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改区域";
        this.form.status= String(response.data.status);
        this.onParentColdChanged();
        this.onColdTypeChanged();
      });
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.subId != undefined) {
            this.form.updateBy = this.userId;
            updateSmallCold(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getEvery();
            });
          } else {
            this.form.createBy = this.userId;
            addSmallCold(this.form).then(response => {
              if(response.code == 200){
                const cc = response.msg;
                this.$modal.msgSuccess(cc);
                this.open = false;
                this.getEvery();
              }
            });
          }
        }
      });
    },

    /** 删除按钮操作 */
    handleDelete(row) {
      const subIds = row.subId || this.ids;
      this.$modal.confirm('是否确认删除此数据项？').then(function() {
        return deleteSmallCold(subIds);
      }).then(() => {
        this.getEvery();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('storage/smallCold/export', {
        ...this.queryParams
      }, `smallCold_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
<style>
.map2{
  width: 100%;
  height: 500px;
}
#openSome{
  margin-left: 20px;
  background-color: #BBFFFF;
}
#openSome:hover{
  background-color: #87CEEB; /* 更深的蓝色 */
  color: white; /* 鼠标悬停时字体颜色为白色 */
}
</style>
